// battlenpc.txt
// Used exclusively for the Battlegrounds scenarios
// A npc that responds to pc's commands, retreats when it is either heavily wounded or it's
// side is losing in the current battleground, walks to a waypoint, and hunt's down enemies.
// Messages:
//	 100 - Creature just looks for a target and attacks it.
//	 0 to 3 - Creature follows this number party member around.
//	 4 - Creature stops moving.
//	 5 - Creature tries to heal or bless nearby allies.
//	 6 - Changes the creature's X,Y movement destination.
//	 200 - Character doesn't move until it sees an enemy.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0 and creature is hostile, alerts creature. If friendly, waits for command.
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1 - Waypoint to walk to.
//   Cell 2 - 
//   Cell 3 - Dialogue node to start with if talked to. If left at 0, script will give a
//     simple text saying the creature's moral.
//   Cell 4 - If non-zero, this creature can use hostile abilities.
//   Cell 5 - If non-zero, this creature can use defensive abilities.
//   Cell 6 - For friendly only. Which squadron this character is part of.

begincreaturescript;

variables;

short i,target,message,last_abil_time,last_abil_time2,r1,x,y;
string str;

body;

beginstate INIT_STATE;
	set_char_script_mode(ME,3); // Act even at distance
	
	message = -1; // Default to no message
	
	set_flag(my_number(),0,0); // Personal SDF
	
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	
	if (get_memory_cell(0) == 0) {
		if (get_attitude(ME) > 4)
			alert_char(ME); // All enemy characters are instantly alerted
	}
	
	if (get_memory_cell(6) > 0)
		add_char_to_group(ME,get_memory_cell(6));
break;

beginstate DEAD_STATE;
	if (get_attitude(ME) < 5) // Decrease team moral if friendly or neutral dies
		inc_flag(0,0,-1);
	else inc_flag(0,0,1); // Otherwise increase team moral for killing an enemy
break;

beginstate START_STATE;
	// If combat has started and I have no message, default to regular advance
	if (get_flag(0,4) > 0 && message == -1)
		message = 100;
	
	// If I have a message, save it as variable 'message'
	if (my_current_message() >= 0)
		message = my_current_message();
	
	// If I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
		else set_target(ME,-1);
	}
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
	}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		set_state(3);
	}
	
	// If message is 200, stop moving, wait for enemy to come into sight
	if (message == 200)
		stop_moving(ME);
	
	// If message is 0 to 3, follow that number party member
	if (message >= 0 && message < 4) {
		if (get_memory_cell(0) != 2) {
			if (char_ok(message) == FALSE) {
				fidget(ME,25);
			}
			else approach_char(ME,message,1 + get_ran(1,0,2));
		}
	}
	
	// If message is 4, stop moving, creature stays put
	if (message == 4) {
		stop_moving(ME);
		set_mobility(ME,0);
	}
	else {
		if (get_memory_cell(0) != 2) {
			set_mobility(ME,1);
		}
	}
	
	// If message is 6, change X,Y destination
	if (message == 6) {
		if (get_flag(my_number(),0) == 1) {
			x = get_flag(0,1);
			y = get_flag(0,2);
			set_flag(my_number(),0,0);
		}
		move_to_loc_x_y(ME,x,y);
	}
	
	// Defualt action for friendly character
	if (message == 100 || message == 5 || message > 1000) {
		if (get_flag(0,4) > 0) {
			if (approach_waypoint(ME,get_memory_cell(1),5) == 1)
				fidget(ME,25);
		}
		else return_to_start(ME,0);
	}
	
	// If we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	// If I am wounded or my team's moral is low, flee
	if (get_attitude(ME) < 5) { // Only if friendly
		if ((get_health(ME) < get_max_health(ME) / 3) || (get_flag(0,0) < 1)) {
			if (get_ran(1,0,100) < 40) {
				flee_char(ME,get_target,10);
				end();
			}
		}
	}

	if ((tick_difference(last_abil_time,get_current_tick()) >= 3) &&
	  (get_memory_cell(4) > 0) && (get_ran(1,0,100) < 50) && (can_see_char(1000))) {
	  	r1 = get_ran(1,0,2);
	  	if (r1 == 0) {
			print_named_str(ME,"fires a draining beam.");
			put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(get_target()),
				char_loc_y(get_target()),3);
			run_animation_sound(46);
			set_char_status(get_target(),1,-10,0,1);
			deduct_ap(4);
	  		}
	  	if (r1 == 1) {
			print_named_str(ME,"fires a weakening beam.");
			put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(get_target()),
				char_loc_y(get_target()),3);
			run_animation_sound(65);
			set_char_status(get_target(),2,-10,0,1);
			deduct_ap(4);
	  		}
	  	if (r1 == 2) {
			print_named_str(ME,"fires a slowing beam.");
			put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(get_target()),
				char_loc_y(get_target()),3);
			run_animation_sound(7);
			set_char_status(get_target(),3,-10,0,1);
			deduct_ap(4);
	  		}
		last_abil_time = get_current_tick();
	}
	if ((tick_difference(last_abil_time,get_current_tick()) >= 3) &&
	  (get_memory_cell(5) > 0) && (get_ran(1,0,100) < 50) && (can_see_char(1000))) {
	  	r1 = get_ran(1,0,2);
	  	if (r1 == 0) {
			print_named_str(ME,"is blessed.");
			put_sparkles_on_char(ME,2,10);
			run_animation_sound(4);
			set_char_status(ME,1,10,0,1);
			deduct_ap(4);
	  		}
	  	if (r1 == 1) {
			print_named_str(ME,"is shielded.");
			put_sparkles_on_char(ME,4,10);
			run_animation_sound(4);
			set_char_status(ME,2,10,0,1);
			deduct_ap(4);
	  		}
	  	if (r1 == 2) {
			print_named_str(ME,"speeds up.");
			put_sparkles_on_char(ME,6,10);
			run_animation_sound(12);
			set_char_status(ME,3,10,0,1);
			deduct_ap(4);
	  		}
		last_abil_time = get_current_tick();
	}
	
	// If message is 5, try to do beneficial things to allies
	// Only if combat has started
	if (message == 5) {
		if (get_flag(0,4) > 0) {
			if (get_stat(ME,11) > 0 || get_stat(ME,12) > 0) {
				if (get_stat(ME,11) > 0) {
					if ((get_ran(1,0,100) < 40) && (tick_difference(last_abil_time2,get_current_tick())) >= 5) {
						print_named_str(ME,"hastes nearby allies.");
						put_sparkles_on_char(ME,6,10);
						run_animation_sound(12);
						force_status_nearby(10,5,3,0);
						last_abil_time2 = get_current_tick();
						deduct_ap(4);
					}
				}
				if (get_stat(ME,12) > 0) {
					if ((get_ran(1,0,100) < 40) && (tick_difference(last_abil_time2,get_current_tick())) >= 5) {
						print_named_str(ME,"blesses nearby allies.");
						put_sparkles_on_char(ME,2,10);
						run_animation_sound(4);
						force_status_nearby(10,5,1,0);
						force_status_nearby(10,5,2,0);
						last_abil_time2 = get_current_tick();
						deduct_ap(4);
					}
				}
			}
		}
	}
	
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		clear_buffer(); // Get the team moral and tell the player what it is
		append_string("This soldier's moral is ");
		if (get_flag(0,0) >= 8)
			append_string("high.");
		else if (get_flag(0,0) >= 4)
			append_string("average.");
		else if (get_flag(0,0) < 4)
			append_string("low.");
		get_buffer_text(str);
		print_str_color(str,2);
//		clear_buffer();
//		append_string("Their number is ");
//		append_number(my_number);
//		get_buffer_text(str);
//		print_str_color(str,2);
		set_flag(0,10,my_number());
		run_scenario_script(11);
	}
		else {
			begin_talk_mode(get_memory_cell(3));
		}
break;